<template>
	<view class="content">
		<view class="list">
			<view class="ul2" v-for="(item,index) in order.orderItems">
			    <view class="li"><image :src="baseUrl + item.thumbnail"></image></view>
			    <view class="li">
			        <view class="name">{{item.name}}</view>
			        <view class="zl">重量：{{item.weight}}g</view>
			    </view>
			    <view class="li">¥{{item.price}}</view>
			    <view class="li">x{{item.quantity}}</view>
			    <view class="li">￥{{item.totalPrice}}</view>
			</view>
		</view>
		<view v-if="!show">
		<view class="list">
			<view class="item center">
				<view class="left">服务类型 </view>
			
				<!-- <picker class="right" mode="selector" @change="bindPickerChange" :value="type" :range="serType" range-key="name">
					<view class="uni-input">{{serType[type].name}}</view>
				</picker> -->
				<radio-group @change="radioChange">
					<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in serType" :key="item.id">
						<view>
							<radio color="#D30500" style="transform:scale(0.7)" :value="item.id" :checked="index === type" /> {{item.name}}
						</view>
					</label>
				</radio-group>
			</view>
		</view>
		<view class="list">
			<view class="item">
				<view class="left">提交时间 </view>
			
				<view class="right">{{nowTime}}</view>
			</view>
		</view>
		<view class="list" v-show="type === 1">
			<view class="item">
				<view class="left">退款金额 </view>
			
				<view class="right">￥{{order.amount}}</view>
			</view>
		</view>
		<view class="list">
			<view class="item">
				<view class="left">{{type | text}}</view>
			
				<textarea v-model="form.aftersales.reason" placeholder="您有什么想要说明的..."></textarea>
			</view>
		</view>
		<view class="list">
			<view class="item">
				<view class="left">图片信息 </view>
				<view class="right pics">
					<view class="box" @click="delPic(index)" v-for="item,index in imageList" :key="index">
						<image class="pic" :src="baseUrl + item"></image>
						<view class="del-box"><!-- <image class="del" src="../../static/images/29-29.png"></image> --></view>
					</view>
					<image class="add" @click="goAdd" src="@/static/images/92.png"></image>
				</view>
			</view>
		</view>
		
		<!-- <view class="bottom_total">
			<view class="left">
			</view>
			<view class="right" @click="submit">
				提交
			</view>
		</view> -->
		
		<view class="submit-btn" @click="submit">提交</view>
		</view>
		
		<view v-if="show">
			<view class="list">
				<view class="item pointer">
					<view class="left">物流公司 </view>
				
					<picker class="right picker" mode="selector" @change="bindPickerChange1" :value="type1" :range="companyList" range-key="name">
						<view class="uni-input">{{companyList[type1].name}}</view>
					</picker>
				</view>
			</view>
			<view class="list">
				<view class="item center">
					<view class="left">快递单号 </view>
				
					<input type="text" class="trackNo" v-model="form1.trackingNo" />
				</view>
			</view>
			<!-- <view class="bottom_total">
				<view class="left">
				</view>
				<view class="right" @click="submit1">
					提交
				</view>
			</view> -->
			<view class="submit-btn" @click="submit1">提交</view>
		</view>
	</view>
</template>

<script>
	import urlConfig from '@/common/config.js'
	import {
		orderDetail, replacement, returns, getDeliveryCorpList, transitTabs
	} from '@/api/order.js'
	export default {
		filters: {
			text(val) {
				if(val === 0) {
					return '换货说明'
				} else if (val === 1) {
					return '退款说明'
				}
			}
		},
		data() {
			return {
				orderSn: '',
				order: {},
				baseUrl: this.$baseUrl,
				serType: [
					{
						id: 0,
						name: '换货'
					},
					{
						id: 1,
						name: '退款退货'
					}
				],
				form: {
					aftersales: {
						aftersalesItems: [],
						reason: "",
						images: ""
					},
					orderSn: ""
				},
				type: 0,
				nowTime: '',
				imageList: [],
				show: false,
				form1: {
					aftersalesId: '',
					deliveryCorpId: '',
					trackingNo: ''
				},
				sn: '',
				type1: 0,
				companyList: []
			}
		},
		onLoad(options) {
			this.sn = options.id
			this.form.orderSn = options.id
			this.nowTime = this.formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss')
			if(options.type && options.type == '1') {
				this.show = true
				this.form1.aftersalesId = options.id
				this.sn = options.sn
				// let item = JSON.parse(uni.getStorageSync('item'))
				// this.form.aftersales.reason = item.reason
				// this.form.aftersales.images = item.images
			}
			this.init()
			this.initData()
		},
		methods: {
			init() {
				orderDetail({orderSn: this.sn}).then(res => {
					this.order = res.data.order
					res.data.order.orderItems.forEach(r => {
						let data = {
							orderItem: {
								id: r.id,
							},
							quantity: r.quantity
						}
						this.form.aftersales.aftersalesItems.push(data)
					})
				})
			},
			initData() {
				getDeliveryCorpList().then(res => {
					this.companyList = res.data.deliveryCorps
					this.form1.deliveryCorpId = this.companyList[this.type1].id
				})
			},
			radioChange(e) {
				for (let i = 0; i < this.serType.length; i++) {
				    if(this.serType[i].id == e.detail.value) {
				        this.type = i
				        break
				    }
				}
			},
			bindPickerChange(e) {
				this.type = e.detail.value
			},
			bindPickerChange1(e) {
				this.type1 = e.detail.value
				this.form1.deliveryCorpId = this.companyList[this.type1].id
			},
			goAdd() {
				let that = this
				uni.chooseImage({
					count: 1,
					success: res => {
						uni.uploadFile({
							url: urlConfig + '/public/file',
							filePath: res.tempFilePaths[0],
							name: 'file',
							success: (inner) => {
								try {
									let str = JSON.parse(inner.data)
									that.imageList.push(str.data.url)
								} catch (err) {
									uni.showToast({
										title: err,
										icon: 'none'
									})
								}
							}
						})
					}
				})
			},
			delPic(index) {
				this.imageList.splice(index, 1)
			},
			submit() {
				this.form.aftersales.images = ''
				this.imageList.forEach(r => {
					this.form.aftersales.images += r + ';'
				})
				if(this.type == 0) {
					replacement(JSON.stringify(this.form)).then(res => {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/order/order?type='
							})
						}, 800)
					})
				} else if (this.type == 1) {
					returns(JSON.stringify(this.form)).then(res => {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/order/order?type='
							})
						}, 800)
					})
				}
			},
			submit1() {
				transitTabs(this.form1).then(res => {
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/after/after'
						})
					}, 800)
				})
			},
			formatDate(date, fmt) {
				if (/(y+)/.test(fmt)) {
				    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
				}
				let o = {
				    'M+': date.getMonth() + 1,
				    'd+': date.getDate(),
				    'h+': date.getHours(),
				    'm+': date.getMinutes(),
				    's+': date.getSeconds()
				}
				for (let k in o) {
				    if (new RegExp(`(${k})`).test(fmt)) {
						let str = o[k] + ''
						fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : ('00' + str).substr(str.length))
				    }
				}
				return fmt
			},
		}
	}
</script>

<style lang="scss">
.content {
	padding-bottom: 30rpx;
	background-color: #F8F9FA;
	min-height: 100vh;
}
.submit-btn {
	line-height: 80rpx;
	text-align: center;
	border-radius: 80rpx;
	color: #fff;
	background-color: #D30500;
	margin: 40rpx 30rpx 0;
}
label {
	display: inline-block;
	margin-left: 30rpx;
}
.list {
	padding: 30rpx;
	margin-top: 30rpx;
	background-color: #fff;
}

.list .item {
	display: flex;
	align-items: top;
	justify-content: space-between;
	color: #1a1a1a;
}

.list .item.center {
	align-items: center;
}

.left {
	width: 130rpx;
	margin-right: 20rpx;
}

.trackNo {
	width: calc(100% - 160rpx);
}

textarea {
	height: 200rpx;
	padding: 0;
	margin: 0;
}

.list .pics {
	width: calc(100% - 160rpx);
}

.list .box {
	margin: 0 15rpx 15rpx 0;
	position: relative;
	width: 150rpx;
	height: 150rpx;
	display: inline-block;
}

.list image {
	width: 150rpx;
	height: 150rpx;
}

.list .del-box {
	background: url(~@/static/images/29-29.png) no-repeat center rgba(255, 255, 255, .8);
	background-size: 60%;
	width: 45rpx;
	height: 45rpx;
	position: absolute;
	top: 0;
	right: 0;
}

.bottom_total {
		display: flex;
		align-items: center;
		justify-content: space-between;
		height: 110rpx;
		width: 100%;
		background-color: #ffffff;
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 99999;

		.left {
			padding-left: 30rpx;

			text {
				color: #b81c22;
				font-size: 32rpx;
			}
		}

		.right {
			color: #ffffff;
			letter-spacing: 4rpx;
			width: 300rpx;
			text-align: center;
			height: 110rpx;
			line-height: 110rpx;
			background-color: #b81c22;
		}
	}

.ul2 {
    /* border: 1px solid #EDEDED; */
    background-color: #fff;
    display: flex;
    align-items: center;
}

.ul2 .li:nth-child(1) image {
    margin-right: 20rpx;
	width: 120rpx;
	height: 120rpx;
}

.ul2 .li:nth-child(2) view:nth-child(2) {
    color: #999;
	padding-top: 15rpx;
    /* font-size: 14rpx; */
}

.ul2 .li:nth-child(3) {
    flex-grow: 4;
    text-align: center;
    /* font-size: 14rpx; */
    padding: 0 20rpx;
}

.ul2 .li:nth-child(4) {
    flex-grow: 2;
    padding: 0 20rpx;
    /* font-size: 14rpx; */
}


.ul2 .li:nth-child(5) {
    /* font-size: 14rpx; */
    color: #D30500;
}

.picker {
	width: calc(100% - 160rpx);
}

.pointer {
	background: url(~@/static/images/icon44.png) no-repeat right center;
}
</style>
